package com.ruoyi.carShopService.service.impl;

import com.ruoyi.carShopService.domain.ShopUserDto;
import com.ruoyi.carShopService.mapper.RegisterMapper;
import com.ruoyi.carShopService.service.RegisterService;
import com.ruoyi.carShopService.util.SmsUtil;
import com.ruoyi.system.api.domain.CarMerchant;
import com.ruoyi.system.api.domain.CarMerchantPrincipal;
import com.ruoyi.system.api.domain.CarMerchantVerification;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author yang
 * @version 1.0
 * @data 2024/9/12 19:58
 */
@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RegisterMapper registerMapper;

    @Override
    public int register(ShopUserDto shopUserDto) {
            CarMerchant carMerchant = new CarMerchant();
            CarMerchantPrincipal principal = new CarMerchantPrincipal();
            CarMerchantVerification verification = new CarMerchantVerification();
            carMerchant.setMerName(shopUserDto.getShopName());
            carMerchant.setMerArea(shopUserDto.getShopAddress());
            carMerchant.setImgPath(shopUserDto.getFormFileList3().stream().collect(Collectors.joining(",")));
            carMerchant.setMerAddress(shopUserDto.getShopHome());
            carMerchant.setMerIntroduction(shopUserDto.getMessage());
            principal.setPcpName(shopUserDto.getName());
            principal.setIdNumber(shopUserDto.getIdNum());
            principal.setPhoneNumber(shopUserDto.getPhone());
            principal.setIdCardFront(shopUserDto.getFormFileList1().get(0));
            principal.setIdCardBack(shopUserDto.getFormFileList1().get(1));
            verification.setMerLicenseNumber(shopUserDto.getShopId());
            verification.setMerLicenseImage(shopUserDto.getFormFileList2().stream().collect(Collectors.joining(",")));

        int num1 = registerMapper.insertMerchant(carMerchant);
        principal.setMerId(carMerchant.getId());
        int num2 = registerMapper.insertPrincipal(principal);
        verification.setPcpId(principal.getMpId());
        verification.setMerId(carMerchant.getId());
        int num3 = registerMapper.insertVerification(verification);
        if( num1 == 1 && num2 == 1 && num3 == 1) {
            return 1;
        }
        else{
            return 0;
        }
    }

}
